package cn.net.xinyi.xs.util;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * ��ɵ���Excel�ļ�����
 * 
 * @author caihua
 * 
 */
public class ExcelWriter {

	private WritableWorkbook workbook = null;

	private WritableSheet sheet = null;

	public ExcelWriter() {
	}

	public ExcelWriter(File file) throws Exception {
		workbook = Workbook.createWorkbook(file);
	}

	/**
	 * ��ʼ��Excel
	 * 
	 */
	public ExcelWriter(OutputStream out) {
		try {
			this.workbook = Workbook.createWorkbook(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.sheet = workbook.createSheet("Sheet1", 0);
	}

	/**
	 * ����Excel�ļ�
	 * 
	 * @throws IOException
	 */
	public void export() throws IOException {
		workbook.write();
		try {
			workbook.close();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setColumnWidth(int columnid, int width) {
		sheet.setColumnView(columnid, width);
	}

	public void setRowHigth(int rowid, int higth) throws RowsExceededException {
		sheet.setRowView(rowid, higth);
	}

	public void setRowHigth(int rowid, boolean iswrap)
			throws RowsExceededException {
		sheet.setRowView(rowid, iswrap);
	}

	public void addLabel(int columnid, int rowid, String content)
			throws RowsExceededException, WriteException {
		Label label = new Label(columnid, rowid, content);
		sheet.addCell(label);
	}

	public void addLabel(String content, int columnid, int rowid, int fontsize)
			throws RowsExceededException, WriteException {
		WritableFont wf = new WritableFont(WritableFont.ARIAL, fontsize);
		WritableCellFormat wcf = new WritableCellFormat(wf);
		wcf.setAlignment(jxl.format.Alignment.CENTRE);
		wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
		wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
		wcf.setWrap(true);
		Label label = new Label(columnid, rowid, content, wcf);
		sheet.addCell(label);
	}

	public void addLabel(String content, int columnid, int rowid, int fontsize,
			String align) throws RowsExceededException, WriteException {
		WritableFont wf = new WritableFont(WritableFont.ARIAL, fontsize);
		WritableCellFormat wcf = new WritableCellFormat(wf);
		if (align.equals("left")) {
			wcf.setAlignment(jxl.format.Alignment.LEFT);
		}
		if (align.equals("center")) {
			wcf.setAlignment(jxl.format.Alignment.CENTRE);
		}
		if (align.equals("right")) {
			wcf.setAlignment(jxl.format.Alignment.RIGHT);
		}
		wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
		wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
		wcf.setWrap(true);
		Label label = new Label(columnid, rowid, content, wcf);
		sheet.addCell(label);
	}

	public void addLabel(String content, int columnid, int rowid,
			WritableCellFormat wcf) throws RowsExceededException,
			WriteException {
		Label label = new Label(columnid, rowid, content, wcf);
		sheet.addCell(label);
	}

	public void addLabel(String content, int columnid, int rowid,
			WritableFont wf, Alignment align, VerticalAlignment vAlign,
			Border border, BorderLineStyle lineStyle, boolean wrap)
			throws RowsExceededException, WriteException {
		WritableCellFormat wcf = new WritableCellFormat(wf);
		if (align != null) {
			wcf.setAlignment(align);
		}
		if (vAlign != null) {
			wcf.setVerticalAlignment(vAlign);
		}
		if ((border != null) && (lineStyle != null)) {
			wcf.setBorder(border, lineStyle);
		}
		wcf.setWrap(wrap);
		Label label = new Label(columnid, rowid, content, wcf);
		sheet.addCell(label);
	}

	public void addNumber(String pattern, double content, int columnid,
			int rowid) throws RowsExceededException, WriteException {
		if (pattern == null) {
			WritableCellFormat integerFormat = new WritableCellFormat(
					NumberFormats.INTEGER);
			jxl.write.Number number2 = new jxl.write.Number(columnid, rowid,
					content, integerFormat);
			sheet.addCell(number2);
		} else {
			NumberFormat fivedps = new NumberFormat(pattern);
			WritableCellFormat fivedpsFormat = new WritableCellFormat(fivedps);
			jxl.write.Number number4 = new jxl.write.Number(columnid, rowid,
					content, fivedpsFormat);
			sheet.addCell(number4);
		}
	}

	public void addDate(String pattern, Date date, int columnid, int rowid)
			throws RowsExceededException, WriteException {
		DateFormat customDateFormat = new DateFormat(pattern);
		WritableCellFormat dateFormat = new WritableCellFormat(customDateFormat);
		DateTime dateCell = new DateTime(columnid, rowid, date, dateFormat);
		sheet.addCell(dateCell);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		//ExcelWriter ew = new ExcelWriter(new File("c:\\test.xls"));
	}
}
